<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace backend\controllers;

use builder\helper\App;
use Yii;
use builder\base\BaseController;
use builder\form\FormBuilder;

/**
 * 系统设置
 * @author clevstn
 * @since ym2.0
 */
class SettingController extends BaseController
{
    public $actionVerbs = [
        'view' => ['GET', 'POST'],
    ];

    /**
     * 设置选项卡
     * @return \yii\web\Response|string
     * @throws \Throwable
     */
    public function actionView()
    {
        // 视图构建器
        $viewBuilder = $this->viewBuilder;
        if ($this->isPost) {
            switch ($viewBuilder->builderId) {
                case 'System_Setting': // 系统
                    return $this->system();
                default:
                    return $this->asFail('未知配置，保存失败。');
            }
        } else {
            App::setViewBreadcrumb([
                ['name' => '系统管理'],
                ['name' => '设置', 'url' => 'setting/view'],
            ]);
            // 选项卡
            $viewBuilder->tab
                ->setId('Setting_View')
                ->setTheme('column')
                ->setTitle('设置')
                ->setTabItem([
                    '后台设置' => $this->tabItem->builder($this->system())->selected(),
                ]);

            return $viewBuilder->render();
        }
    }

    /**
     * 系统
     * @return FormBuilder|\yii\web\Response
     * @throws \Throwable
     */
    protected function system()
    {
        $setting = Yii::$app->setting;
        if ($this->isPost) {
            $bodyParams = $this->post;

            $setting->write('SYS.SYS_TLOG', $bodyParams['SYS_TLOG'], '记录操作日志开关')
                ->write('SYS.SYS_SSO', $bodyParams['SYS_SSO'], '一号一人登录开关')
                ->write('SYS.SYS_PROTECT', $bodyParams['SYS_PROTECT'], '后台维护开关')
                ->write('SYS.SYS_PROTECT_TIPS', $bodyParams['SYS_PROTECT_TIPS'], '维护通知内容')
                ->invalidateCache();

            return $this->asOk('【设置/后台设置/系统设置】配置更新成功。');
        } else {
            $form = FormBuilder::instance(['id' => 'System_Setting']);

            return $form->setRootLayout(['class' => 'col-md-6 px-0'])
                ->setFooterLayout(['class' => 'text-left'])
                ->setFooterFixed(false)
                ->setGroup('系统设置', 'system_setting')
                ->setGroupComment('<span class="ym2-text-code">SYS</span>')
                ->setExtraCss([
                    <<<CSS
.checked--red .layui-form-onswitch {
    border-color: rgba(255, 89, 89, 0.08) !important;
    background-color: #ff5959 !important;
}
CSS,
                ])
                ->setFormControl([
                    'SYS_PROTECT' => $this->formControl->sw()->checked($setting->read('SYS.SYS_PROTECT', 0))->comment('<span class="ym2-text-code">SYS.SYS_PROTECT</span>')->uiClass('checked--red')->label('后台维护开关'),
                    'trigger' => $this->formControl->trigger()->when('system_setting', 'SYS_PROTECT', '==', 1)->triggerItems([
                        'SYS_PROTECT_TIPS' => $this->formControl->textarea()->help("<strong>维护提醒示例：</strong><br/>维护提醒：YYYY/mm/dd HH:ii时维护，预计维护时间N个小时。早上HH:ii前完成维护。")->defaultValue($setting->read('SYS.SYS_PROTECT_TIPS'))->comment('<span class="ym2-text-code">SYS.SYS_PROTECT_TIPS</span>')->label('维护通知内容'),
                    ])->uiClass(['col-md-12', 'p-0'])->display(),
                    'SYS_TLOG' => $this->formControl->sw()->checked($setting->read('SYS.SYS_TLOG', 1))->comment('<span class="ym2-text-code">SYS.SYS_TLOG</span>')->label('记录操作日志开关'),
                    'SYS_SSO' => $this->formControl->sw()->checked($setting->read('SYS.SYS_SSO', 1))->comment('<span class="ym2-text-code">SYS.SYS_SSO</span>')->label('一号一人登录开关'),
                ])
                ->setSubmitConfirm()
                ->setResetBtn()
                ->setSubmitBtn('保存配置');
        }
    }
}